package a2022.a20220607;

import java.util.Arrays;

/**
 * @author lenovo
 * @date 2022/6/7
 */
public class a {
    public static void main(String[] args) {
        int[] piles = new int[]{30,11,23,4,20};
        int i = minEatingSpeed(piles, 6);
        System.out.println(i);
    }

    public static int minEatingSpeed(int[] piles, int h) {
        int low = 1;
        Arrays.sort(piles);
        int high = piles[piles.length - 1];
        int k = high;
        while (low < high) {
            //拿到中间值进行判断
            int speed = (high - low) / 2 + low;
            long time = getTime(piles, speed);
            if (time <= h) {
                k = speed;
                high = speed;
            } else {
                low = speed + 1;
            }
        }
        return k;
    }

    public static long getTime(int[] piles, int speed) {
        long time = 0;
        for (int pile : piles) {
            int curTime = (pile + speed - 1) / speed;
            time += curTime;
        }
        return time;
    }

}
